#ifndef HEAD_H
#define HEAD_H
#include<iostream>
using namespace std;

struct node{
    node*pre;
    node*nx;
    int data;
};

class arraylist{
private:
    node*head;
    node*tail;
    int len;
public:
    ~arraylist();
    arraylist();
    void showlist(node*phead);
    void creatlist(int len);
    void processlist();
};
void arraylist::processlist(){

    node*p=head;
    node*q;//保存节点前一个位置的节点的指针
    node*r;//偶数链表的移动指针
    node*even=new node;//奇数链表
    node*eventail;
    int i=1;
    even->pre=NULL;
    r=even;
    int counter=0;
    while(p->nx!=NULL&&p->nx->nx!=NULL){
        q=p;
        p=p->nx;
        if(!(i%2)){
           q->nx=p->nx;
           p->nx->pre=q;

           p->pre=r;
           r->nx=p;
           r=p;
           counter++;
        }
        i++;
    }
    r->nx=NULL;
    eventail=r;
    showlist(even);


    node*t=eventail;
    p=tail;
    while(t->pre!=NULL){
        q=t->pre;
        p->nx=t;
        t->pre=p;
        p=t;
        t=q;
    }
    tail=t->nx;
    tail->nx=NULL;
    delete even;
}

arraylist::arraylist(){
    cout<<"input len:";
    cin>>len;
    creatlist(len);
    showlist(head);
    processlist();
    showlist(head);
    cout<<endl;
}

void arraylist::creatlist(int len){

    node*p;
    node*q;
    p=head=tail=new node;
    head->pre=NULL;
    int i=0;
    while(i<len){
        q=p;
        tail=p=p->nx=new node;
        p->data=++i;
        p->pre=q;
    }
    tail->nx=NULL;

}

arraylist::~arraylist(){
    node*p;
    while(head!=NULL){
        p=head;
        head=head->nx;
        delete p;
    }
    head=tail=NULL;
}

void arraylist::showlist(node *phead){
    node*p=phead;
    cout<<endl;
    while(p->nx!=NULL){
        p=p->nx;
        cout<<p->data<<"\t";
    }
}

#endif // HEAD_H
